﻿--[[
    @brief   ui logic for user_guide ui
    @author  hqzheng
    @version 1.0
    @date    2017-07-08
--]]

user_guide_view = 
{
    user_guide_ctn_id = 0;
    user_guide_ctn_ptr = nil;
    user_guide_web = 0;
    cancle_btn_id = 0;
    init = function()
        base_utility.log("user_guide_view init.");
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[user_guide_view] %s", msg), core_msg);
    end;

    open_user_guide_page = function(param)
        user_guide_model.init_config(param);

        user_guide_model.has_finish_user_guide = false;
        local mainwnd_id = ui_utility.get_ctrl_id_by_cid(CID_MAINFRAME_Dialog_MainFrame);
        user_guide_view.load_dialog_component(mainwnd_id);
        user_guide_view.user_guide_ctn_id = ui_utility.get_ctrl_id_by_path(
            mainwnd_id, "UserGuideDialog");
        ui_utility.setproperty_by_id(user_guide_view.user_guide_ctn_id, "MoveWith", "frmMain");
        user_guide_view.user_guide_ctn_ptr = ui_utility.get_ctrl_ptr_by_id(
            user_guide_view.user_guide_ctn_id);
        user_guide_view.cancle_btn_id = ui_utility.get_ctrl_id_by_path(
            user_guide_view.user_guide_ctn_id, "CloseBtnCtn\\Btn_Cancle");
        ui_utility.setproperty_by_id(user_guide_view.user_guide_ctn_id, "Visible", "true");
        user_guide_view.load_web_component();
        
        if user_guide_model.show_param ~= nil then 
            if user_guide_model.show_param.report_type ~= nil then
                user_guide_model.user_guide_report(user_guide_model.show_param.report_type, "show");
            end
            comm_center.send_broadcast("i_broadcast_show_user_guide", 
                                   {focus_home_banner = user_guide_model.show_param.need_focus_home;
                                   });
        end

        if nil ~= user_guide_view.user_guide_ctn_ptr then
            user_guide_view.user_guide_ctn_ptr:SetParent(wegame_mainframe_view.dialog_ptr);
            user_guide_view.user_guide_ctn_ptr:Show(TRUE);
            user_guide_view.user_guide_ctn_ptr:BringToTop();
            user_guide_view.adjust_size();
        end
    end;

    adjust_size = function ()
        local main_wnd_ptr = ui_utility.get_ctrl_ptr_by_cid(CID_MAINFRAME_Dialog_MainFrame);
        local user_guide_ptr = ui_utility.get_ctrl_ptr_by_id(user_guide_view.user_guide_ctn_id);
        if nil ~= main_wnd_ptr and nil ~= user_guide_ptr then
            local rct = main_wnd_ptr:GetRectEx(TRUE, TRUE);
          
            local width = rct.right - rct.left;
            local height = rct.bottom - rct.top;
            rct.left = 0;
            rct.right = width;
            rct.top = 0;
            rct.bottom = height;
            user_guide_ptr:Move(rct);
        end
    end;

    close_user_guide = function ()
        if user_guide_view.user_guide_ctn_id ~= 0 then
            user_guide_view.unload_web_component();
            user_guide_view.unload_dialog_component();
            layout_mgr.remove_ctrl(user_guide_view.user_guide_ctn_id);
            -- 恢复主窗口拖放
            ui_utility.setproperty_by_cid(CID_MAINFRAME_Dialog_MainFrame, "SizeBox", "true");

            if user_guide_model.show_param ~= nil and 
               user_guide_model.show_param.report_type ~= nil then
                user_guide_model.user_guide_report(user_guide_model.show_param.report_type,
                                  "close", user_guide_model.cur_page_index);
            end
            
            user_guide_view.user_guide_ctn_id = 0;
        end

        user_guide_model.is_showing_user_guide = false;
        if user_guide_model.show_param ~= nil and
           user_guide_model.show_param.guide_close_callback ~= nil then
            user_guide_model.show_param.guide_close_callback();
        end
    end;

    on_close_user_guide = function ()
        user_guide_view.close_user_guide();
    end;

    -- 加载web组件
    load_web_component = function()
        local web_component_ptr = ui_utility.get_ctrl_ptr_by_path(
            user_guide_view.user_guide_ctn_id, "Ctn_WebArea");
        if web_component_ptr ~= nil then
            local is_load = web_component_ptr:IsLoad();
            if is_load ~= TRUE then
                local result = web_component_ptr:LoadContent();
                if result ~= TRUE then
                    -- 加载web组件失败，800ms之后重试
                    local tpfui =
                        base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");
                    tpfui:AddInterval('user_guide_view.delay_load_web_timer', 800);
                end
            end
            user_guide_view.init_web_frame();
        end
    end;

    load_dialog_component = function (wnd_id)
        local component_ptr = ui_utility.get_ctrl_ptr_by_path(wnd_id, "pnlUserGuide");
        if component_ptr ~= nil then
            local is_load = component_ptr:IsLoad();
            if is_load ~= TRUE then
                local result = component_ptr:LoadContent();
                if result ~= TRUE then
                end
            end
        end
    end;

    -- 卸载web组件
    unload_web_component = function()
        local web_component_ptr = ui_utility.get_ctrl_ptr_by_path(
            user_guide_view.user_guide_ctn_id, "Ctn_WebArea");
        if web_component_ptr ~= nil then
            web_component_ptr:UnLoadContent();
        end
    end;

    unload_dialog_component = function ()
        local mainwnd_id = ui_utility.get_ctrl_id_by_cid(CID_MAINFRAME_Dialog_MainFrame);
        user_guide_view.user_guide_uo_id = ui_utility.get_ctrl_id_by_path(
            mainwnd_id, "pnlUserGuide");
        local component_ptr = ui_utility.get_ctrl_ptr_by_id(user_guide_view.user_guide_uo_id);
        if component_ptr ~= nil then
            component_ptr:UnLoadContent();
        end
    end;

    -- 延迟加载web组件
    delay_load_web_timer = function()
        user_guide_view.inner_log("due to load failer, again load_web_component.");
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RemoveInterval('user_guide_view.delay_load_web_timer');
        local web_component_ptr = ui_utility.get_ctrl_ptr_by_path(
            user_guide_view.user_guide_ctn_id, "Ctn_WebArea");
        if web_component_ptr ~= nil then
            local is_load = web_component_ptr:IsLoad();
            if is_load ~= TRUE then
                web_component_ptr:LoadContent()
            end
            user_guide_view.init_web_frame();
        end
    end;

    -- 初始化web框架
    init_web_frame = function()
        local mainwnd_id = ui_utility.get_ctrl_id_by_cid(CID_MAINFRAME_Dialog_MainFrame);
        local webctn_ptr = ui_utility.get_ctrl_ptr_by_path(user_guide_view.user_guide_ctn_id,
            "Ctn_WebArea\\QBWebBrowser");

        if nil == webctn_ptr then
            base_utility.log("navigateUrl, webctn_ptr is nil");
            return;
        end

        webctn_ptr:Show(TRUE);
        webctn_ptr:BringToTop();

        local qbweb_ptr = tolua.cast(webctn_ptr, "Tenio::IQBWebBrowser");
        if nil == qbweb_ptr then
            base_utility.log("navigateUrl, qbweb_ptr is nil");
            return false;
        end
        local const_ui_str = U8toCUIStr(user_guide_model.show_param.url);
        qbweb_ptr:Navigate(const_ui_str);
        -- 先让主窗口不能拖放
        ui_utility.setproperty_by_cid(CID_MAINFRAME_Dialog_MainFrame, "SizeBox", "false");

        user_guide_model.is_showing_user_guide = true;
        if user_guide_model.show_param ~= nil and
           user_guide_model.show_param.guide_show_callback ~= nil then
            user_guide_model.show_param.guide_show_callback();
        end
    end;

    show_cancle_btn = function ()
        ui_utility.setproperty_by_id(user_guide_view.cancle_btn_id, "Visible", "true");
    end;

    on_navigate_complete = function ()
        user_guide_view.show_cancle_btn();
    end;

    on_navigate_failed = function ()
        user_guide_view.show_cancle_btn();
    end;
};

user_guide_view.init();